﻿Imports Aricie.Collections
Imports System.Linq

Namespace Aricie.DNN.Services.Url.FriendlyUrl.Controls.UICommands


    Friend MustInherit Class BaseFUPCommand
        Implements IFUPCOmmand

        Public Property CommandArgs As String = String.Empty Implements IFUPCOmmand.CommandArgs
        Public MustOverride Sub RunCommand(data As UrlGroups) Implements IFUPCOmmand.RunCommand
    End Class


    Friend Class ReorderGroupsCommand
        Inherits BaseFUPCommand
        Public Overrides Sub RunCommand(data As UrlGroups)
            Dim EditedGroupOrderChanged As Boolean = False
            Dim NewGroupOrder As New SerializableList(Of UrlGroup)
            For Each entry As String In CommandArgs.Split(New String() {","}, StringSplitOptions.RemoveEmptyEntries)
                If (entry.StartsWith("NEW:")) Then
                    Dim NewGroupPosition As Integer = Convert.ToInt32(New String(entry.SkipWhile(Function(c) c <> ":"c).Skip(1).ToArray()))
                    ' ok il faut trouver quel est l'élément qui nous intéresse
                    NewGroupOrder.Add(GroupManagement.FUPGroupManagerFactory.GetGroup(data.GroupsAvailable(NewGroupPosition)))

                    If data.EditedGroup >= NewGroupOrder.Count - 1 Then
                        data.EditedGroup += 1
                        EditedGroupOrderChanged = True
                    End If
                Else
                    If (data.EditedGroup = Convert.ToInt32(entry)) Then
                        data.EditedGroup = NewGroupOrder.Count
                        EditedGroupOrderChanged = True
                    End If
                    NewGroupOrder.Add(data.Groups(Convert.ToInt32(entry)))
                End If
            Next

            data.Groups = NewGroupOrder
            If EditedGroupOrderChanged Then data.SetInnerPropertyEditor()
            data.BindGroups()
        End Sub
    End Class

    Friend Class DeleteGroupCommand
        Inherits BaseFUPCommand
        Public Overrides Sub RunCommand(data As UrlGroups)
            Dim DeletionChangedEditGroupOrder As Boolean = False
            Dim Position As Integer = Convert.ToInt32(CommandArgs)
            data.Groups.RemoveAt(Position)

            If (data.EditedGroup = Position) Then
                data.EditedGroup = -1
                DeletionChangedEditGroupOrder = True
            End If

            If data.EditedGroup > Position Then
                data.EditedGroup -= 1
                DeletionChangedEditGroupOrder = True
            End If

            If DeletionChangedEditGroupOrder Then data.SetInnerPropertyEditor()

            data.BindGroups()
        End Sub
    End Class

    Friend Class EditGroupCommand
        Inherits BaseFUPCommand
        Public Overrides Sub RunCommand(data As UrlGroups)
            data.EditedGroup = Convert.ToInt32(CommandArgs)
            data.SetInnerPropertyEditor()
            data.BindGroups()
        End Sub
    End Class

End Namespace